package cn.edu.hstc.wm.adapter;

import java.util.List;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;
import cn.edu.hstc.wm.R;
import cn.edu.hstc.wm.bean.Good;
import cn.edu.hstc.wm.ui.fragment.StoreItemMenu;

/**
 * StoreItemMenuAdapter 店铺下显示该店铺所有菜单的适配器
 * 
 */
public class StoreItemMenuAdapter extends BaseAdapter {

	public static final String TAG = "StoreItemMenuAdapter";
	Context mContext;
	List<Good> menus;
	ViewHolder holder;
	public CallBack mCallbacks;

	public StoreItemMenuAdapter(Context context, List<Good> menus) {
		this.mContext = context;
		this.menus = menus;
	}

	public StoreItemMenuAdapter(FragmentActivity activity, List<Good> menus2, StoreItemMenu storeItemMenu) {
		this.mContext = activity;
		this.menus = menus2;

		if (!(storeItemMenu instanceof CallBack)) {
			throw new IllegalStateException("storeItemMenu must implement StoreItemMenuAdapter's callbacks.");
		}
		mCallbacks = (CallBack) storeItemMenu;
	}

	@Override
	public int getCount() {
		return menus.size();
	}

	@Override
	public Object getItem(int position) {
		return menus.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.store_item_menu_fragment_item, null);
			initView(holder, convertView, position);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		setView(holder, menus.get(position));

		return convertView;
	}

	private void setView(final ViewHolder holder, Good menu) {
		holder.menuName.setText(menu.getName());
		// holder.saleCount.setText(menu.getSaleCount() + ""); //月销售量
		holder.price.setText(menu.getPrice() + "");
		// holder.grade.setRating(menu.getGrade()); //菜单评分

	}

	private void initView(final ViewHolder holder, View convertView, final int position) {
		holder.menuName = (TextView) convertView.findViewById(R.id.tv_store_item_menu_name);
		holder.saleCount = (TextView) convertView.findViewById(R.id.tv_store_item_menu_sales_count);
		holder.price = (TextView) convertView.findViewById(R.id.tv_store_item_price);
		holder.grade = (RatingBar) convertView.findViewById(R.id.ratingbar_store_item_menu_grade);
		holder.selectCountView = (View) convertView.findViewById(R.id.fl_store_item_menu_select_count);
		holder.selectCountText = (TextView) convertView.findViewById(R.id.tv_store_item_menu_select_count);
		holder.priceView = (View) convertView.findViewById(R.id.ll_store_item_menu_price);

		// 监听到priceView被点击后,显示购物车栏,显示份数,显示总价
		// 点击时菜单名就显示对话框内容
		// holder.selectCountView.setVisibility(View.GONE); // 隐藏显示份数

		// FIXME 覆盖显示了内容?? 把点击次数和是否显示放在goods对象里貌似可以
		holder.priceView.setOnClickListener(new OnClickListener() {
			int count = 1;

			@Override
			public void onClick(View v) {
				holder.selectCountView.setVisibility(View.VISIBLE); // 显示每个菜单的个数
				holder.selectCountText.setText("" + count++);
				// 回调
				mCallbacks.onMenuItemClick(position);
			}
		});

	}

	public final class ViewHolder {
		// 菜单名,销量,价格
		public TextView menuName, saleCount, price;
		public RatingBar grade;
		/** 当前点击该菜单的份数 */
		public View selectCountView;
		public TextView selectCountText;
		public View priceView;
	}

	public interface CallBack {

		/**
		 * @param position
		 *            点击的位置
		 */
		public void onMenuItemClick(int position);
	}

}
